
const service = axios.create({
    baseURL: 'http://121.43.98.111:8080',
    timeout: 5000,
    headers: {
        'Content-Type':'application/json'
    }
})

service.interceptors.request.use(
    (config) => {
        const token = localStorage.getItem('authToken')
        if (token) {
            config.Headers.Authorization=`Bearer ${token}`
        }

        return config
    },
    (error) => {
        return Promise.reject({
            msg: '发送请求失败',
            type:'error'
        })
    }
)

service.interceptors.response.use(
    (response) => {

        console.log('get response successfully:'+JSON.stringify(response.data))

        if (response.data.success !== true) {

            return Promise.reject({
                msg: response.data.message,
                type:'error'
            })
        }
        return response.data
    },
    (error) => {
        console.log('error occurred in response interceptor,error.response:'+JSON.stringify(error.response))

        /*
        let msg = '网络异常，请检查连接'
        
        if (error.response) {
            switch (error.response.status) {
                case 401:
                    msg = '用户名或密码错误'
                    break
                case 403:
                    msg = '没有访问权限'
                    break
                case 404:
                    msg = '请求的资源不存在'
                    break
                case 500:
                    msg = '服务器内部错误'
                    break
                default:
                    msg=error.response.data?.message || `请求失败(${error.response.status})`
            }
        }*/
        
        return Promise.reject({
            msg: error.response.data.message || '网络异常，请检查连接',
            type:'error'
        })
    }
)

export default service